package com.example.demo.controller;


import com.example.demo.entity.Book;
import com.example.demo.mapper.BookMapper;
import com.example.demo.utils.ResponseWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {
@Autowired
    BookMapper bookMapper;

    @RequestMapping("addBook")
    public ResponseWrapper addBook(Book book){
        System.out.println(book.getId());
        bookMapper.insert(book);
        return ResponseWrapper.markSuccessButNoData();
    }

    @RequestMapping("updateBook")
    public void updateBook(Book book){
        bookMapper.updateByPrimaryKeySelective(book);
    }

    @RequestMapping("getBook")
    public ResponseWrapper getbook(){
        return ResponseWrapper.markSuccess(bookMapper.selectAll());
    }

    @RequestMapping("deleteBook")
    public ResponseWrapper deleteBook(@RequestParam("id") Integer id){
        bookMapper.deleteByPrimaryKey(id);
        return ResponseWrapper.markSuccessButNoData();
    }

    /**
     * 分页获取数据
     * @return
     */
    @RequestMapping("listBook")
    public ResponseWrapper listBook(@RequestParam("start") Integer start){
        start=start*10;
        PageHelper.offsetPage(start,10);
        List<Book> books = bookMapper.selectAll();
        PageInfo pageInfo = new PageInfo(books,10);
//        pageInfo.setTotal(total);
        return ResponseWrapper.markSuccess(pageInfo);
    }
}
